﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.Business.Others;
using CapstoneProjectAEReader.GUIs.TranslationGUI;

namespace CapstoneProjectAEReader.GUIs.SettingGUI
{
    /// <summary>
    /// Interaction logic for Setting.xaml
    /// </summary>
    public partial class Setting : UserControl
    {
        #region Attributes

        private DAO _dao;
        private SyncManager _syncManager;
        private FileHandler _fileHandler;
        private MainWindow _parent;
        private const string FontHeader = "fontHeader";
        private const string FontType = "fontType";
        private const string FontSizeLb = "fontSizeLb";
        private const string DisplayHeader = "displayHeader";
        private const string ReadingMode = "readingMode";
        private const string DayMode = "dayMode";
        private const string NightMode = "nightMode";
        private const string LanguageLb = "language";
        private const string Services = "services";
        private const string BtUpload = "btUpload";
        private const string BtDownLoad = "btDownload";
        private const string BtSave = "saveButton";
        private const string BtCancel = "cancelButton";
        private const string OnlineStorage = "onlineStorage";
        private const string EbookTabName = "ebookTab";
        private const string NoteTabName = "noteTab";
        private const string SettingName = "settingTab";

        #endregion


        public Setting(DTO dto, MainWindow parent)
        {
            _parent = parent;
            _dao = new DAO(dto);
            _fileHandler = new FileHandler();
            _syncManager = new SyncManager(0);
            InitializeComponent();
            ReloadFormLanguage();
        }

        public Grid GetSettingTab()
        {
            return mainGrid;
        }

        public void ReloadFormLanguage()
        {
            gbFont.Header = ResourceReader.GetResourceContent(FontHeader);
            gbDisplay.Header = ResourceReader.GetResourceContent(DisplayHeader);
            gbOnlineStorage.Header = ResourceReader.GetResourceContent(OnlineStorage);
            lbFontSize.Content = ResourceReader.GetResourceContent(FontSizeLb);
            lbFontType.Content = ResourceReader.GetResourceContent(FontType);
            lbLanguage.Content = ResourceReader.GetResourceContent(LanguageLb);
            lbReadingMode.Content = ResourceReader.GetResourceContent(ReadingMode);
            lbServices.Content = ResourceReader.GetResourceContent(Services);
            //btCancel.Content = ResourceReader.GetResourceContent(BtCancel);
            btDownload.Content = ResourceReader.GetResourceContent(BtDownLoad);
            btSave.Content = ResourceReader.GetResourceContent(BtSave);
            btUpload.Content = ResourceReader.GetResourceContent(BtUpload);
            rbDay.Content = ResourceReader.GetResourceContent(DayMode);
            rbNight.Content = ResourceReader.GetResourceContent(NightMode);
        }

        private void HandleBtUpload(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                _syncManager.UploadHandler(_dao.Dto);
                System.Windows.Threading.Dispatcher.Run();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
        }


        public void closeStatusBar()
        {
            _parent.SetStatus("");
        }

        private void HandleBtDownload(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(() =>
            {
                _syncManager.DownloadHandler();
                System.Windows.Threading.Dispatcher.Run();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
            if (Utilities.CheckFileExist(Constants.StorageStructure.ApplicationData))
            {
                _dao.Dto = _fileHandler.ReadFile(Constants.StorageStructure.ApplicationData);
                _parent.Dto = _dao.Dto;
            }
        }

        private void HandleSaveBt(object sender, RoutedEventArgs e)
        {
            _dao.Dto.SettingDetail.FontSize = int.Parse(cbFontSize.SelectionBoxItem.ToString());
            _dao.Dto.SettingDetail.SelectedFontType = cbFontType.SelectionBoxItem.ToString();
            if(rbDay.IsChecked == true)
            {
                _dao.Dto.SettingDetail.ReadingMode = Constants.SettingSupporter.DayReadingMode;
            }
            else
            {
                _dao.Dto.SettingDetail.ReadingMode = Constants.SettingSupporter.NightReadingMode;
            }
            _dao.Dto.SettingDetail.ApplicationLanguage =
                Constants.SettingSupporter.SupportedLanguages[cbLanguage.SelectionBoxItem.ToString()];
            string oldEbookTabName = ResourceReader.GetResourceContent(EbookTabName);
            string oldSettingTabName = ResourceReader.GetResourceContent(SettingName);
            string oldNoteTabName = ResourceReader.GetResourceContent(NoteTabName);
            ResourceReader.Initialize(_dao.Dto.SettingDetail.ApplicationLanguage);
            _parent.ReloadEbookTabLanguage(oldEbookTabName);
            _parent.ReloadNoteTabLanguage(oldNoteTabName);
            _parent.ReloadSettingTabLanguage(oldSettingTabName);
            _parent.ReloadEbooksTabLanguage();
            ReloadFormLanguage();
            _parent.ReloadFormLanguage();
            DAO.CommitData(_dao.Dto);
        }
    }
}
